<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>聊天系统 - 登录/注册</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f2f5;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .container {
            background: white;
            padding: 40px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            width: 100%;
            max-width: 400px;
        }
        h1 {
            color: #1890ff;
            margin-bottom: 30px;
            text-align: center;
        }
        .tabs {
            display: flex;
            margin-bottom: 20px;
            border-bottom: 1px solid #eee;
        }
        .tab {
            flex: 1;
            padding: 10px;
            text-align: center;
            cursor: pointer;
        }
        .tab.active {
            color: #1890ff;
            border-bottom: 2px solid #1890ff;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            color: #666;
        }
        .form-group input {
            width: 100%;
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
        }
        .form-group input:focus {
            outline: none;
            border-color: #1890ff;
        }
        .button {
            width: 100%;
            padding: 12px;
            background: #1890ff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        .button:hover {
            background: #40a9ff;
        }
        .error {
            color: #ff4d4f;
            margin-top: 10px;
            text-align: center;
        }
        .success {
            color: #52c41a;
            margin-top: 10px;
            text-align: center;
        }
        .forgot-password {
            text-align: right;
            margin-top: 10px;
        }
        .forgot-password a {
            color: #1890ff;
            text-decoration: none;
            font-size: 14px;
        }
        .forgot-password a:hover {
            text-decoration: underline;
        }
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.5);
            justify-content: center;
            align-items: center;
        }
        .modal-content {
            background: white;
            padding: 20px;
            border-radius: 8px;
            width: 90%;
            max-width: 400px;
        }
        .modal-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }
        .modal-close {
            cursor: pointer;
            font-size: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>聊天系统</h1>
        <div class="tabs">
            <div class="tab active" onclick="switchTab('login')">登录</div>
            <div class="tab" onclick="switchTab('register')">注册</div>
        </div>
        
        <form id="loginForm" onsubmit="handleLogin(event)">
            <div class="form-group">
                <label for="loginAccount">账号</label>
                <input type="text" id="loginAccount" required minlength="3" maxlength="20">
            </div>
            <div class="form-group">
                <label for="loginPassword">密码</label>
                <input type="password" id="loginPassword" required minlength="6">
            </div>
            <button type="submit" class="button">登录</button>
            <div id="loginError" class="error"></div>
        </form>

        <form id="registerForm" style="display: none;" onsubmit="handleRegister(event)">
            <div class="form-group">
                <label for="registerAccount">账号</label>
                <input type="text" id="registerAccount" required minlength="3" maxlength="20">
            </div>
            <div class="form-group">
                <label for="registerPassword">密码</label>
                <input type="password" id="registerPassword" required minlength="6">
            </div>
            <div class="form-group">
                <label for="confirmPassword">确认密码</label>
                <input type="password" id="confirmPassword" required minlength="6">
            </div>
            <button type="submit" class="button">注册</button>
            <div id="registerError" class="error"></div>
            <div id="registerSuccess" class="success"></div>
        </form>
    </div>

    <!-- 忘记密码模态框 -->
    <div id="forgotPasswordModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h3>找回密码</h3>
                <span class="modal-close" onclick="hideForgotPasswordModal()">&times;</span>
            </div>
            <form id="forgotPasswordForm" onsubmit="handleForgotPassword(event)">
                <div class="form-group">
                    <label for="forgotAccount">账号</label>
                    <input type="text" id="forgotAccount" required pattern="[0-9]{7}" title="请输入7位数字账号">
                </div>
                <div class="form-group">
                    <label for="forgotEmail">QQ邮箱</label>
                    <input type="email" id="forgotEmail" required pattern="[1-9][0-9]{4,10}@qq\.com" title="请输入有效的QQ邮箱">
                </div>
                <button type="submit" class="button">发送重置链接</button>
                <div id="forgotPasswordError" class="error"></div>
                <div id="forgotPasswordSuccess" class="success"></div>
            </form>
        </div>
    </div>

    <script>
        function switchTab(tab) {
            document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
            document.querySelector(`.tab:${tab === 'login' ? 'first-child' : 'last-child'}`).classList.add('active');
            document.getElementById('loginForm').style.display = tab === 'login' ? 'block' : 'none';
            document.getElementById('registerForm').style.display = tab === 'register' ? 'block' : 'none';
            // 清除所有错误和成功消息
            document.querySelectorAll('.error, .success').forEach(el => el.textContent = '');
        }

        async function handleLogin(event) {
            event.preventDefault();
            const account = document.getElementById('loginAccount').value;
            const password = document.getElementById('loginPassword').value;

            try {
                const response = await fetch('/api/auth/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ account, password })
                });

                const data = await response.json();
                if (response.ok) {
                    localStorage.setItem('token', data.token);
                    window.location.href = '/chat.html';
                } else {
                    document.getElementById('loginError').textContent = data.msg || '登录失败';
                }
            } catch (err) {
                document.getElementById('loginError').textContent = '服务器错误';
            }
        }

        async function handleRegister(event) {
            event.preventDefault();
            const account = document.getElementById('registerAccount').value;
            const password = document.getElementById('registerPassword').value;
            const confirmPassword = document.getElementById('confirmPassword').value;

            if (password !== confirmPassword) {
                document.getElementById('registerError').textContent = '两次输入的密码不一致';
                return;
            }

            try {
                const response = await fetch('/api/auth/register', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ account, password })
                });

                const data = await response.json();
                if (response.ok) {
                    document.getElementById('registerSuccess').textContent = '注册成功！请使用新账号登录。';
                    document.getElementById('registerError').textContent = '';
                    // 3秒后自动切换到登录页
                    setTimeout(() => {
                        switchTab('login');
                    }, 3000);
                } else {
                    document.getElementById('registerError').textContent = data.msg || '注册失败';
                    document.getElementById('registerSuccess').textContent = '';
                }
            } catch (err) {
                document.getElementById('registerError').textContent = '服务器错误';
                document.getElementById('registerSuccess').textContent = '';
            }
        }

        function showForgotPasswordModal() {
            document.getElementById('forgotPasswordModal').style.display = 'flex';
        }

        function hideForgotPasswordModal() {
            document.getElementById('forgotPasswordModal').style.display = 'none';
            document.getElementById('forgotPasswordForm').reset();
            document.getElementById('forgotPasswordError').textContent = '';
            document.getElementById('forgotPasswordSuccess').textContent = '';
        }

        async function handleForgotPassword(event) {
            event.preventDefault();
            const account = document.getElementById('forgotAccount').value;
            const email = document.getElementById('forgotEmail').value;

            try {
                const response = await fetch('/api/auth/forgot-password', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ account, email })
                });

                const data = await response.json();
                if (response.ok) {
                    document.getElementById('forgotPasswordSuccess').textContent = data.msg;
                    document.getElementById('forgotPasswordError').textContent = '';
                    // 3秒后关闭模态框
                    setTimeout(hideForgotPasswordModal, 3000);
                } else {
                    document.getElementById('forgotPasswordError').textContent = data.msg || '发送失败';
                    document.getElementById('forgotPasswordSuccess').textContent = '';
                }
            } catch (err) {
                document.getElementById('forgotPasswordError').textContent = '服务器错误';
                document.getElementById('forgotPasswordSuccess').textContent = '';
            }
        }
    </script>
</body>
</html> 